<?php

namespace App\Models\Ddaman;

use App\Models\xwbei\Student;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\common\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class WorkflowBacklog extends Base
{
    use HasFactory;

    protected $table = "workflow_backlog";

    //const CREATED_AT = null;
    //const UPDATED_AT = null;

    protected $fillable = [
        'guid','wid','uid','sponsor_id','step','is_audit','remarks'
    ];


    public $rule = [
        'name' => 'required|string|between:2,100',
        'logo' => 'required',
    ];

    public $editRule = [
        'id' => 'required',
        'name' => 'required|string|between:2,100',
        'logo' => 'required',
    ];

    public $message = [
        'id.required' => '标识必须',
        'name.required' => '分类名称必须填写',
        'logo.required'=>"请上传品牌logo",
    ];


    /*
     * 保存
     * */
    public function changes( $wid ,$design_id, $user_id,$step ,$send_id)
    {
        $where2 = [
            ['wid',$wid],
            ['uid',$user_id],
            ['step',$step]
        ];
        $info = WorkflowBacklog::where($where2)->first();
        $isAudit = $send_id === $user_id && $step == 1 ? 1 : 0;
        if ($info){
            $result = WorkflowBacklog::where('id',$info->id)
                ->update([
                    'id'=>$info->id,
                    'is_audit'=>$isAudit,
                    'remarks'=>"",
                ]);
        }else{
            $result = WorkflowBacklog::create([
                'guid'=>getGuid(),
                'wid'=>$wid,
                'uid'=>$user_id,
                'is_audit'=>$isAudit,
                'sponsor_id'=>$send_id,
                'design_id'=>$design_id,
                'step'=>$step,
            ]);
        }

        if (!$result){
            $this->error = "更新失败 N100011";
            return false;
        }

        return true;
    }


    /*
     * 获取列表  我的代办
     * @param uid 2222 333 555  666 777  88 99
     * */
    public function getTodoList( $param )
    {
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $identify = get_identify() ? (int)get_identify() : 0;
        $where = [
            ['b.is_audit',0],
            ['w.is_filed',0],
            ['b.uid',get_uuid()]
        ];
        //dump($identify);die;

        if (isset($param['sponsor']) && $param['sponsor']){
            $sponsor = trim($param['sponsor']);
            $where['s.username'] = ['like', "%$sponsor%"];
        }

        switch ($param){
            case isset($param['search_time']) && $param['search_time']:
                //dump($param);die;
                $where['w.update_time'] = ['between time',$param['search_time']];
                break;
            case isset($param['keywords']) && $param['keywords']:
                $keywords = trim($param['keywords']);
                $where['w.tags|w.work_no'] = ['like', "%$keywords%"];
                break;
        }
        //dump($where);die;
        $select = [
            'b.id','b.guid','b.uid',"b.wid",'b.step','d.title',"d.sign","b.sponsor_id","b.identify","b.company_id","b.school_id","b.student_id","b.created_at","b.updated_at",
            "w.work_no","w.step as w_step","w.design_id","w.tags","w.created_at as w_created_at",
            "f.title as f_title"
        ];
        $listData = DB::table('workflow_backlog as b')
            ->leftJoin('workflow as w', function ($join) {
                $join->on('b.wid', '=', 'w.guid');
                $join->on('b.step', '=', DB::raw('w.step+1'));
            })
            ->leftJoin('workflow_design as d','w.design_id','=','d.guid')
            ->leftJoin('workflow_flow as f', function ($join) {
                $join->on('w.design_id', '=', 'f.design_id');
                $join->on('w.step', '=', 'f.step');
            })
            ->select($select)
            ->where($where)
            ->orderBy('b.id','desc')
            ->paginate($limit);
        $listData->transForm(function ($item){
            $item->f_title = $item->w_step === 0 ? "重新提交": $item->f_title;
            $item->is_sponsor = get_uuid() == $item->sponsor_id && $item->w_step === 0  ? true : false;
            return $item;
        });

        return $listData;
    }


    /*
     * 我的流程跟踪
     * */
    public function getProcessList( $param )
    {
        $limit = isset($param['limit']) ? $param['limit'] : 12;
        $where = [
            ['b.uid',get_uuid()]
        ];

        if (isset($param['sponsor']) && $param['sponsor']){
            $sponsor = trim($param['sponsor']);
            $where['s.username'] = ['like', "%$sponsor%"];
        }

        $select = [
            "w.guid", DB::raw("(w.step+1) as w_step"),
            "w.work_no","w.design_id","w.tags","w.is_filed","w.created_at",
            "b.wid","d.title","d.sign","f.title as f_title"
        ];

        $listData = DB::table('workflow_backlog as b')
            ->select($select)
            ->leftJoin('workflow as w','b.wid','=','w.guid')
            ->leftJoin('workflow_design as d','w.design_id',"=",'d.guid')
            ->leftJoin('workflow_flow as f',function ($join){
                $join->on('w.design_id','=','f.design_id');
                $join->on('b.step','=','f.step');
            })
            ->where($where)
            ->orderBy('b.id','desc')
            ->paginate($limit);
        //dump($listData);die;

        $listData->transForm(function ($item){
            $item->f_title = '【'.$item->w_step.'】' . $item->f_title;
            if ($item->is_filed === 1){
                $item->todo_user = "【已归档】";
            }else{
                $item->w_step = $item->w_step + 1;
                $item->todo_user = "审核中";
                //$item->todo_user = $this->getBacklogUser($item->wid,$item->w_step);
            }
            return $item;
        });

        return $listData;
    }


    /*
     * 获取代办人
     * */
    public function getBacklogUser( $wid ,$step)
    {
        $info = DB::table('workflow_backlog')
            ->select("b.id","m.username","m.uid")
            ->leftJoin('member as m','b.uid=m.guid','left')
            ->where([
                'b.wid'=>$wid,
                'b.step'=>$step
            ])
            ->order('b.step desc')
            ->find();
        return getFullName($info['username'],$info['uid']);
    }



}
